<script setup lang="ts">
import { ref, computed, watch } from 'vue';
import { Menu, Document, Setting, Search, Refresh, ArrowDown } from '@element-plus/icons-vue';
import { useI18n } from 'vue-i18n';
import { useRouter, useRoute } from 'vue-router';
// 引入Element Plus语言包
import zhCn from 'element-plus/es/locale/lang/zh-cn';
import en from 'element-plus/es/locale/lang/en';
import { ElConfigProvider } from 'element-plus';

const activeTab = ref('board');
const { t, locale } = useI18n();
const router = useRouter();
const route = useRoute();
// Element Plus区域设置
const elementLocale = ref(locale.value === 'zh-CN' ? zhCn : en);

// 监听语言变化，更新Element Plus的语言
watch(
  () => locale.value,
  (val) => {
    elementLocale.value = val === 'zh-CN' ? zhCn : en;
  }
);

// 计算当前活动的标签
const currentRouteName = computed(() => route.name as string);

// 判断是否显示头部导航栏 - 在设置页面、卡片编辑页面和卡片预览页面不显示
const showHeader = computed(() => {
  // Add route.path check to ensure we're checking both the route name and path
  const editorPaths = ['/editor', '/editor/'];
  if (route.path.startsWith('/editor/') || editorPaths.includes(route.path)) {
    return false;
  }
  return !['Settings', 'CardEditor', 'ViewCard', 'EditCard'].includes(currentRouteName.value);
});

// 切换语言
const toggleLanguage = () => {
  const newLocale = locale.value === 'zh-CN' ? 'en-US' : 'zh-CN';
  locale.value = newLocale;
  
  // 更新localStorage存储
  localStorage.setItem('language', newLocale);
  
  // 同时更新general-settings中的语言设置
  try {
    const generalSettings = localStorage.getItem('general-settings');
    if (generalSettings) {
      const settings = JSON.parse(generalSettings);
      settings.language = newLocale;
      localStorage.setItem('general-settings', JSON.stringify(settings));
    }
  } catch (error) {
    console.error('更新general-settings中的语言设置失败:', error);
  }
  
  // 更新应用配置
  import('./services/configService').then(module => {
    module.getConfig().then(config => {
      config.general.language = newLocale;
      module.saveConfig(config).then(success => {
        if (success) {
          // 使用Element Plus的消息提示显示切换成功
          import('element-plus').then(({ ElMessage }) => {
            ElMessage.success(newLocale === 'zh-CN' ? '语言已切换为中文' : 'Language switched to English');
          });
        } else {
          console.error('保存语言配置失败');
        }
      });
    });
  });
};

// 刷新窗口
const refreshWindow = () => {
  window.location.reload();
};

// 打开设置页面 - 修改为导航到Settings路由
const openSettingsDialog = () => {
  router.push({ name: 'Settings' });
};

// 导航到不同页面
const navigateTo = (routeName: string) => {
  router.push({ name: routeName });
};
</script>

<template>
  <el-config-provider :locale="elementLocale">
    <div class="app-wrapper">
      <el-header class="app-header" v-if="showHeader">
        <div class="header-container">
          <div class="header-left">
            <div class="board-icon">
              <el-icon>
                <Menu />
              </el-icon>
            </div>
            <div class="board-tabs">
              <div 
                class="tab" 
                :class="{ active: currentRouteName === 'Home' }"
                @click="navigateTo('Home')"
              >
                <strong>{{ t('header.kanbanBoard') }}</strong>
              </div>
              <div 
                class="tab" 
                :class="{ active: currentRouteName === 'TableBoard' }"
                @click="navigateTo('TableBoard')"
              >
                {{ t('header.tableBoard') }}
              </div>
              <div 
                class="tab" 
                :class="{ active: currentRouteName === 'BucketBoard' }"
                @click="navigateTo('BucketBoard')"
              >
                {{ t('header.bucketBoard') }}
              </div>
              <div 
                class="tab" 
                :class="{ active: currentRouteName === 'FavoritesBoard' }"
                @click="navigateTo('FavoritesBoard')"
              >
                {{ t('header.favoritesBoard') }}
              </div>
              
              <div 
                class="tab" 
                :class="{ active: currentRouteName === 'TrashBin' }"
                @click="navigateTo('TrashBin')"
              >
                {{ t('header.trashBin') }}
              </div>
              
            </div>
          </div>
          <div class="header-right">
            <div class="header-actions">
              <div class="action-button" @click="refreshWindow" :title="t('header.refresh')">
                <el-icon>
                  <Refresh />
                </el-icon>
              </div>
             
              <div class="action-button" @click="openSettingsDialog" :title="t('header.settings')">
                <el-icon>
                  <Setting />
                </el-icon>
              </div>
              <div class="action-button" :title="locale === 'zh-CN' ? 'EN' : 'CN'" @click="toggleLanguage">
                <span style="font-size: 12px;">{{ locale === 'zh-CN' ? 'EN' : 'CN' }}</span>
              </div>
            
            </div>

          </div>
        </div>
      </el-header>
      <div class="app-content">
        <router-view />
      </div>
    </div>
  </el-config-provider>
</template>

<style scoped>
.app-wrapper {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100%;
  overflow: hidden;
}

.app-content {
  flex: 1;
  overflow-x: hidden;
  overflow-y: auto;
}

.app-header {
  height: 48px;
  background-color: #fff;
  border-bottom: 1px solid #e4e7ed;
  padding: 0;
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 100%;
  padding: 0 16px;
}

.header-left {
  display: flex;
  align-items: center;
}

.board-icon {
  margin-right: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.board-tabs {
  display: flex;
  align-items: center;
  height: 100%;
}

.tab {
  padding: 0 16px;
  height: 48px;
  display: flex;
  align-items: center;
  cursor: pointer;
  color: #606266;
  transition: all 0.3s;
  border-bottom: 2px solid transparent;
  gap: 4px;
}

.tab.active {
  border-bottom: 2px solid #409eff;
  color: #409eff;
}

.tab:hover:not(.active) {
  color: #409eff;
}

.add-tab {
  width: 30px;
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #909399;
  transition: all 0.3s;
}

.add-tab:hover {
  color: #409eff;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.action-button {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #606266;
  transition: all 0.3s;
}

.action-button:hover {
  background-color: #f2f6fc;
  color: #409eff;
}

.action-button.more {
  font-weight: bold;
  font-size: 18px;
}


.language-icon {
  font-size: 16px;
}
</style>
